﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace XLua
{
    [CustomEditor(typeof(LuaBehavior)),CanEditMultipleObjects]
    public class LuaBehaviorEditor : Editor {

        private LuaBehavior _lua;

        void OnEnable()
        {
            _lua = target as LuaBehavior;
        }

        public override void OnInspectorGUI ()
        {
			base.OnInspectorGUI();

			if(!string.IsNullOrEmpty(_lua.luaPath) && GUILayout.Button("Focus Lua File")){
				if(System.IO.File.Exists(Application.dataPath + "/Lua/"+_lua.luaPath+".lua")){
					EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Object>("Assets/Lua/"+_lua.luaPath+".lua"));
				}
			}

            GUILayout.Space(10f);
            var dragArea = GUILayoutUtility.GetRect (0f, 50f, GUILayout.ExpandWidth (true));  
            GUIContent title = new GUIContent ("将Assets/Lua文件夹下面的lua拖到这里");  
            GUI.Box (dragArea, title);
            DropEvent(dragArea);
        }

        void DropEvent(Rect dragArea){
            Event aEvent;  
            aEvent = Event.current;  
            switch (aEvent.type) {  
                case EventType.DragUpdated:  
                case EventType.DragPerform:  
                    if (!dragArea.Contains (aEvent.mousePosition)) {  
                        break;  
                    }  

                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;  
                    if (aEvent.type == EventType.DragPerform) {  
                        DragAndDrop.AcceptDrag ();  

                        for (int i = 0; i < DragAndDrop.objectReferences.Length; ++i) {  
                            UnityEngine.Object temp = DragAndDrop.objectReferences [i];  
                            if (temp != null) {
                                string url = GetObjectPath (temp);
                                if(!string.IsNullOrEmpty(url) && (string.IsNullOrEmpty(_lua.luaPath) || !_lua.luaPath.Equals(url))){
                                    _lua.luaPath = url;
                                    UnityEditor.EditorUtility.SetDirty(_lua);
                                    if (!Application.isPlaying && !string.IsNullOrEmpty(_lua.gameObject.scene.name)){
                                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                                    }
                                }
                                break;  
                            }  
                        }  
                    }
                    Event.current.Use ();  
                    break;  
                default:  
                    break;  
            }  
        }

        string GetObjectPath (Object obj)
        {
            string url = AssetDatabase.GetAssetPath (obj);
            if(url.IndexOf("Assets/Lua")==-1) return "";
            url = url.Replace("Assets/Lua/", "");
            url = url.Replace(".lua","");
            url = url.Replace(".lua.txt","");
            return url;
        }
    }
}